package com.deng.maintenance.dao.impl;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.deng.common.SQLHelper;
import com.deng.common.vo.OrganizationVo;
import com.deng.maintenance.dao.IOrganizationIndicate;
import com.deng.maintenance.vo.OrganizationIndicateVo;

public class OrganizationIndicateImpl extends SQLHelper implements
		IOrganizationIndicate {

	public int deleteOrganizationIndicate(OrganizationIndicateVo vo) {
		// TODO Auto-generated method stub
		return 0;
	}

	public List<OrganizationIndicateVo> getAllOrganizationIndicate(
			OrganizationVo o_vo) {
		try{
			Connection conn = getConnect();
			ResultSet rs = queryData(conn,"select * from KPI_INDICATE_VS_ORGANIZATION	" +
					"	right join KPI_INDICATE	" +
					"	on KPI_INDICATE_VS_ORGANIZATION.indicate_id = KPI_INDICATE.id	" +
					"	and organization_id = "+o_vo.id);
			List<OrganizationIndicateVo> list = new ArrayList<OrganizationIndicateVo>();
			while(rs.next()){
				OrganizationIndicateVo vo = new OrganizationIndicateVo();
				vo.organization_id = o_vo.id;
				vo.organization_name = o_vo.name;
				vo.indicate_id = rs.getInt("id");
				vo.indicate_name = rs.getString("name");
				if(rs.getBigDecimal("bh") != null){
					vo.bh = rs.getBigDecimal("bh").toString();
				}
				if(rs.getBigDecimal("bl") != null){
					vo.bl = rs.getBigDecimal("bl").toString();
				}
				if(rs.getBigDecimal("bm") != null){
					vo.bm = rs.getBigDecimal("bm").toString();
				}
				if(rs.getBigDecimal("th") != null){
					vo.th = rs.getBigDecimal("th").toString();
				}
				if(rs.getBigDecimal("tl") != null){
					vo.tl = rs.getBigDecimal("tl").toString();
				}
				if(rs.getBigDecimal("wh") != null){
					vo.wh = rs.getBigDecimal("wh").toString();
				}
				if(rs.getBigDecimal("wl") != null){
					vo.wl = rs.getBigDecimal("wl").toString();
				}
				vo.test_id = rs.getInt("test_id");
				vo.test_name = rs.getString("test_name");
				
				
				if(rs.getBigDecimal("weight") != null){
					vo.weight = ((BigDecimal)rs.getBigDecimal("weight")).toString();
				}else{
					vo.weight = "0";
				}
				
				
				vo.indicate_description = rs.getString("description");
				vo.unit = rs.getString("unit");
				vo.formula = rs.getString("formula");
				
//				vo.selected = rs.getInt("selected");
				list.add(vo);
			}
			freeConnection(conn);
			return list;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	
	}

	public int createOrganizationIndicate(OrganizationIndicateVo vo) {//创建一个
		Connection conn = getConnect();
		int i = updateData(conn,"insert into KPI_INDICATE_VS_ORGANIZATION " +
				"	(organization_id,indicate_id,indicate_name,weight)" +
				"	values("+vo.organization_id+","+vo.indicate_id+",'"+vo.indicate_name+"',"+new BigDecimal(vo.weight)+")");
		freeConnection(conn);
		return i;
	}

	public List<OrganizationIndicateVo> updateOrganizationIndicate(
			OrganizationIndicateVo vo) {
		return null;
	}

	public int updateInsertOrganizationIndicateByType(
			OrganizationIndicateVo vo) {
		int i = 0;
		try{
			StringBuffer updateStr = new StringBuffer("update KPI_INDICATE_VS_ORGANIZATION " +
					"	set test_id = "+vo.test_id+"," +
					"	test_name = '"+vo.test_name+"'");
			if(vo.bh != null){
				updateStr.append(",	bh = "+new BigDecimal(vo.bh));
			}
			if(vo.bl != null){
				updateStr.append(",	bl = "+new BigDecimal(vo.bl));
			}
			if(vo.bm != null){
				updateStr.append(",	bm = "+new BigDecimal(vo.bm));
			}
			if(vo.th != null){
				updateStr.append(",	th = "+new BigDecimal(vo.th));
			}
			if(vo.tl != null){
				updateStr.append(",	tl = "+new BigDecimal(vo.tl));
			}
			if(vo.wh != null){
				updateStr.append(",	wh = "+new BigDecimal(vo.wh));
			}
			if(vo.wl != null){
				updateStr.append(",	wl = "+new BigDecimal(vo.wl));
			}
			updateStr.append(",	where organization_id = "+vo.organization_id+
					"  	and  indicate_id = "+vo.indicate_id);
			Connection conn = getConnect();
			i = updateData(conn,updateStr.toString());
			freeConnection(conn);
			if(i == 0){//失败
				updateStr = new StringBuffer("insert into KPI_INDICATE_VS_ORGANIZATION " +
						"	(organization_id,indicate_id,indicate_name," +
						"	weight,test_id,test_name," +
						"	bh,bl,bm," +
						"	th,tl,wh,wl)" +
						"	values("+vo.organization_id+"," +vo.indicate_id+",'"+vo.indicate_name+"',"+
						new BigDecimal(vo.weight)+","+
						vo.test_id+",'"+vo.test_name+"',");
				if(vo.bh != null){
					updateStr.append(new BigDecimal(vo.bh)+",");
				}else{
					updateStr.append("NULL ,");
				}
				if(vo.bl != null){
					updateStr.append(new BigDecimal(vo.bl)+",");
				}else{
					updateStr.append("NULL ,");
				}
				if(vo.bm != null){
					updateStr.append(new BigDecimal(vo.bm)+",");
				}else{
					updateStr.append("NULL ,");
				}
				if(vo.th != null){
					updateStr.append(new BigDecimal(vo.th)+",");
				}else{
					updateStr.append("NULL ,");
				}
				if(vo.tl != null){
					updateStr.append(new BigDecimal(vo.tl)+",");
				}else{
					updateStr.append("NULL ,");
				}
				if(vo.wh != null){
					updateStr.append(new BigDecimal(vo.wh)+",");
				}else{
					updateStr.append("NULL ,");
				}
				if(vo.wl != null){
					updateStr.append(new BigDecimal(vo.wl)+",");
				}else{
					updateStr.append("NULL ,");
				}
				updateStr.append(")");
				conn = getConnect();
				i = updateData(conn,updateStr.toString());
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		return i;
	}

	public int updateInsertOrganizationIndicateByWeight(
			OrganizationIndicateVo vo) {
		Connection conn = getConnect();
		int i = updateData(conn,"insert into KPI_INDICATE_VS_ORGANIZATION " +
				"	(organization_id,indicate_id,indicate_name,weight)" +
				"	values("+vo.organization_id+","+vo.indicate_id+",'"+vo.indicate_name+"',"+new BigDecimal(vo.weight)+")");
		freeConnection(conn);
		return i;
	}

}
